package cn.new7as.cas5s.ui.repay;


import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ActivityLifecycle;
import com.hjq.http.listener.OnHttpListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.List;

import cn.new7as.cas5s.ui.easy.netapi.ConfigApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.databinding.ActivityRepaysBinding;
import cn.new7as.cas5s.anhjkfu.OnConfirmListener;
import cn.new7as.cas5s.anhjkfu.PreparePop;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.ApplyPrepareApi;
import cn.new7as.cas5s.ui.easy.netapi.PayChannelApi;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.easy.netapi.OrderContentApi;
import cn.new7as.cas5s.ui.easy.netapi.PayUrlApi;
import cn.new7as.cas5s.ui.easy.netapi.PrepareDataApi;
import cn.new7as.cas5s.ui.easy.netapi.ShowPrepareApi;


public class RepaysActivity extends BaseActivity {


    private ActivityRepaysBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRepaysBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setop("Pending Repayment");
        initV();
    }

    OrderApi.Data data;

    @Override
    public void initV() {
        super.initV();
        data = (OrderApi.Data) getIntent().getExtras().getSerializable("ddba");

        if (data.fo == 0) getorderdeCOntent(data);
        else {
            showprepare(data);
            int i = Utils.getordStaus(data);
            getc(i);
            Drawable drawable = null;
            if (i == 1) {
                binding.osta.setText("Overdue for " + data.overdueDays + " days");
                drawable = getResources().getDrawable(R.mipmap.iconstop);
                binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
                binding.llsettle.setVisibility(View.VISIBLE);

                binding.llpaycontent.setVisibility(View.VISIBLE);
                getChanel(data);
                binding.osta.setBackgroundResource(R.drawable.overdueo);
                binding.osta.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.iconstop, 0, 0, 0);

            } else if (i == 2) {
                binding.osta.setText("Repayment Due in " + data.expireDays + " Days");
                drawable = getResources().getDrawable(R.mipmap.iconpay);
                binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
                binding.llsettle.setVisibility(View.VISIBLE);
                binding.llpaycontent.setVisibility(View.VISIBLE);
                getChanel(data);
                binding.osta.setBackgroundResource(R.drawable.payo);
                binding.osta.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.iconpay, 0, 0, 0);

            }
            // binding.osta.setCompoundDrawables(drawable, null, null, null);
            binding.extendamount.setText(data.extensionRepaymentAmount.replace("₹", ""));
            binding.availableloanamount.setText(data.repaymentAmount.replace("₹", ""));

            binding.channels.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showbottom();
                }
            });
        }

        binding.settle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPay(1);
            }
        });
        binding.extend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPay(2);
            }
        });
        binding.subproof.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.goextra(RepaysActivity.this, SubProofActivity.class, data.tradeNo);
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (prepareid == 0) return;
                show();
                EasyHttp.get(RepaysActivity.this).api(new PrepareDataApi(getunique(), prepareid)).request(new OnHttpListener<HttpData<PrepareDataApi.PreDataData>>() {


                    @Override
                    public void onHttpSuccess(@NonNull HttpData<PrepareDataApi.PreDataData> result) {
                        hide();
                        new XPopup.Builder(RepaysActivity.this).moveUpToKeyboard(false).enableDrag(true).isViewMode(true).isDestroyOnDismiss(true)

                                .asCustom(new PreparePop(RepaysActivity.this, result.getResult()).setPopupCallback(new OnConfirmListener() {
                                    @Override
                                    public void onConfirm(String rscore) {

                                    }

                                    @Override
                                    public void nextloan(String rscore) {
                                        EasyHttp.post(RepaysActivity.this).api(new ApplyPrepareApi(getunique(), data.tradeNo, rscore, prepareid)).request(new OnHttpListener<HttpData<Object>>() {


                                            @Override
                                            public void onHttpSuccess(@NonNull HttpData<Object> result) {
                                                binding.verifyme.setEnabled(false);
                                                binding.verifyme.setText("Already booked");
                                                binding.preparetips.setText("Your appointment is confirmed. Please pay within 24 hours for it to take effect immediately.");
                                                hide();
                                            }

                                            @Override
                                            public void onHttpFail(@NonNull Throwable throwable) {
                                                deal(throwable);
                                            }
                                        });
                                    }
                                })).show();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });

            }
        });
    }

    private void getc(int i) {

        EasyHttp.get(new ActivityLifecycle(this))
                .api(new ConfigApi())
                .request(new OnHttpListener<HttpData<ConfigApi.ConfigData>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<ConfigApi.ConfigData> result) {


                        if (i == 1)
                            binding.tips.setText(result.getResult().overdueRepaymentMind);
                        else binding.tips.setText(result.getResult().repaymentRemind);

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {

                    }
                });
    }

    int sele = 0;

    void showbottom() {
        new XPopup.Builder(this).isDestroyOnDismiss(true)
                .asBottomList("Repayment Channels", strings, null, -1, new OnSelectListener() {
                    @Override
                    public void onSelect(int position, String text) {
                        sele = position;
                        binding.channels.setText(strings[sele]);
                    }
                }).show();
    }

    private void getorderdeCOntent(OrderApi.Data datas) {
        EasyHttp.post(this).api(new OrderContentApi(getunique(), datas.productCategorySubCode)).request(new OnHttpListener<HttpData<OrderContentApi.OrderContentData>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<OrderContentApi.OrderContentData> result) {
                data = result.getResult().repaymentRecord;
                showprepare(data);
                int i = Utils.getordStaus(data);
                getc(i);
                Drawable drawable = null;
                if (i == 1) {
                    binding.osta.setText("Overdue for " + data.overdueDays + " days");
                    drawable = getResources().getDrawable(R.mipmap.iconstop);
                    binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
                    binding.llsettle.setVisibility(View.VISIBLE);

                    binding.llpaycontent.setVisibility(View.VISIBLE);
                    getChanel(data);
                    binding.osta.setBackgroundResource(R.drawable.overdueo);

                } else if (i == 2) {
                    binding.osta.setText("Repayment Due in " + data.expireDays + " Days");
                    drawable = getResources().getDrawable(R.mipmap.iconpay);
                    binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
                    binding.llsettle.setVisibility(View.VISIBLE);
                    binding.llpaycontent.setVisibility(View.VISIBLE);
                    getChanel(data);
                    binding.osta.setBackgroundResource(R.drawable.payo);
                }
                binding.osta.setCompoundDrawables(drawable, null, null, null);
                binding.extendamount.setText(data.extensionRepaymentAmount.replace("₹", ""));
                binding.availableloanamount.setText(data.repaymentAmount.replace("₹", ""));

                binding.channels.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showbottom();
                    }
                });
            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }

    int prepareid;

    private void showprepare(OrderApi.Data data) {
        EasyHttp.post(this).api(new ShowPrepareApi(getunique(), data.tradeNo)).request(new OnHttpListener<HttpData<Object>>() {

            @Override
            public void onHttpSuccess(@NonNull HttpData<Object> result) {
                if (result.getResult() != null) {
                    if (Double.valueOf(result.getResult() + "") != 0) {
                        binding.verifyme.setEnabled(true);
                        binding.preparetips.setText("");

                    } else {
                        binding.verifyme.setEnabled(false);
                        binding.verifyme.setText("Already booked");
                        binding.preparetips.setText("Your appointment is confirmed. Please pay within 24 hours for it to take effect immediately.");

                    }
                    binding.verifyme.setVisibility(View.VISIBLE);
                    prepareid = Integer.parseInt((result.getResult() + "").replace(".0",""));
                } else {

                }
            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }

    private String[] strings;
    List<PayChannelApi.Data> result1;

    private void getChanel(OrderApi.Data data) {
        EasyHttp.get(this).api(new PayChannelApi(getunique(), data.tradeNo)).request(new OnHttpListener<HttpData<List<PayChannelApi.Data>>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<List<PayChannelApi.Data>> result) {
                result1 = (List<PayChannelApi.Data>) result.getResult();
                if (!result1.isEmpty()) {

                    strings = new String[result1.size()];
                    for (int i = 0; i < result1.size(); i++) {
                        strings[i] = "Options" + (i + 1);
                    }
                    binding.channels.setText(strings[0]);
                }
            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }


    private void getPay(int type) {
        show();
        EasyHttp.post(this).api(new PayUrlApi(getunique(), result1.get(sele).code, type, data.tradeNo)).request(new OnHttpListener<HttpData<String>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<String> result) {
                Utils.goextra(RepaysActivity.this, PayForthActivity.class, result.getResult());
                hide();
            }


            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }


}